public class CountSort {
    public static void countSort(int[] array){
        int min = array[0];
        int max = array[0];

        for (int i = 1; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
            }
            if (array[i] > max) {
                max = array[i];
            }
        }

        int[] count = new int[max-min+1];

        for (int i = 0; i < array.length; i++) {
            int index = array[i]-min;
            count[index]++;
        }

        int k = 0;
        for (int i = 0; i < count.length; i++) {
            while (count[i] != 0) {
                array[k] = i + min;
                k++;
                count[i]--;
            }
        }
    }
}
